<template>
  <div class="box">
    <h2>🎂 欢迎登陆社区</h2>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="账号" prop="username">
        <el-input
          type="text"
          v-model="ruleForm.username"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          v-model="ruleForm.password"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >提交</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
      
    </el-form>
  </div>
</template>
<script>
import { userLogin } from "../../api/auth/auth";
export default {
  
  data() {
    return {
      loading: false,
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "账号不能为空", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 5 到 15 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 5 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    test(){
      test(this.userTest).then(res=>{
        const result = res.data;
        console.log(result.msg);
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //校验通过后进行登陆
          this.loading = true;
          this.$store.dispatch("login",this.ruleForm).then(res=>{
            if(res.code == 0){
              this.$message({
                message: res.msg,
                type: 'success',
              })
              this.$router.push("/");
            }else{
                this.$message({
                message: res.msg,
                type: 'error',
              })
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
h2 {
  text-align: center;
  margin-bottom: 20px;
  font-size: 20px;
}
.box {
  border-radius: 15px;
  width: 500px;
  margin: 0 auto;
  margin-top: 40px;
}
.demo-ruleForm {
  width: 400px;
}
</style>